package DAO;

import java.util.List;

import org.hibernate.*;
import org.hibernate.classic.Session;


import Beans.CajaDiariaBean;
import HibernateUtils.HibernateUtil;
import Model.CajaDiaria;

public class HibernateCajaDiariaDAO {
	private static HibernateCajaDiariaDAO instancia = null;
	private static SessionFactory sf = null;

	public static HibernateCajaDiariaDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateCajaDiariaDAO();
		} 
		return instancia;
	}
	
	public void update(CajaDiariaBean entidad){
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		
		session.update(entidad);
		
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public void save(CajaDiariaBean entidad){
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		
		session.save(entidad);
		
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	
	 public List<CajaDiariaBean> findAll(Class clazz) {
	        List<CajaDiariaBean> objects = null;
	        Session session = HibernateUtil.getSessionFactory().openSession();
	        try {
	        	
	            Query query = session.createQuery("from CajaDiariaBean");
	            objects = query.list();
	        } catch (HibernateException e) {
	            
	        } finally {
	        	session.close();
	        }
	        return objects;
	    }

	public CajaDiariaBean getCajaDelDia(String fechaHoy) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Query q=session.createQuery("select c from CajaDiariaBean c where c.fecha=?").setString(0, fechaHoy);
		CajaDiariaBean caja=(CajaDiariaBean) q.uniqueResult();
		return caja;
		
	}

	public CajaDiariaBean buscarCajaDiaria(CajaDiariaBean ab) {
		Session session=sf.openSession();
		CajaDiariaBean cajaDiaria=(CajaDiariaBean) session.get(CajaDiariaBean.class,ab.getIdCajaDiaria());
		session.close();
		return cajaDiaria;
	}

	
	
	 
}
